#include "TeaMakerMsg.h"

// Local Includes
#include "TeaMakerPingMsg.h"

// ------------------------------------------------------------
// Construction & Destruction
// ------------------------------------------------------------
CTeaMakerMsg::CTeaMakerMsg(TeaMakerMessageType messageType/* = UnknownMessage*/)
	: m_MessageType(messageType)
{
	
}

CTeaMakerMsg::~CTeaMakerMsg()
{
	
}

// ------------------------------------------------------------
// Static Functions
// ------------------------------------------------------------
CTeaMakerMsg* CTeaMakerMsg::getMessageFromDataStream(QDataStream &dataStream)
{
	CTeaMakerMsg *pMessage = NULL;
	quint8 nMessageType = 0;
	TeaMakerMessageType messageType = UnknownMessage;

	dataStream >> nMessageType;
	messageType = ((TeaMakerMessageType)nMessageType);

	switch(messageType)
	{
	case PingMessage:
		pMessage = new CTeaMakerPingMsg;
		break;

	default:
		pMessage = new CTeaMakerMsg;
		break;
	}

	pMessage->serialiseMessage(dataStream, false);

	return pMessage;
}

CTeaMakerPingMsg* CTeaMakerMsg::createPingMessage()
{
	return new CTeaMakerPingMsg;
}

// ------------------------------------------------------------
// Member Functions
// ------------------------------------------------------------
bool CTeaMakerMsg::serialiseMessage(QDataStream &dataStream, bool bSave)
{
	if(bSave)
	{
		QByteArray baMessage;
		QDataStream dataStreamMessage(&baMessage, QIODevice::WriteOnly);

		// Insert a placeholder for the data-size, followed by the MessageID.
		dataStreamMessage << ((quint8)m_MessageType);

		// Pass the message-specific serialisation on.
		if(!serialiseMessageData(dataStreamMessage, bSave))
			return false;

		const int nDataCount = (baMessage.count() - sizeof(quint32));

		// Write the final message.
		dataStream.writeBytes(baMessage.data(), baMessage.count());
	}
	else
	{
		// We don't need to serialise the MessageID as we will already know what we are
		// based on the constructor TeaMakerMessageType argument.
		// We still need to serialise the Message Data.
		if(!serialiseMessageData(dataStream, bSave))
			return false;
	}

	return true;
}

bool CTeaMakerMsg::serialiseMessageData(QDataStream &dataStream, bool bSave)
{
	Q_UNUSED(dataStream);
	Q_UNUSED(bSave);

	return true;
}
